/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package toolz;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 *
 * @author Bunkowski
 */
public class WatershedTransformation {
private double[][] jcfMap;
private double[][] corMap;
int xDim, yDim;
int nrOfElements;

    public WatershedTransformation(double[][] jcfMap, double[][] corMap) {
        this.jcfMap = jcfMap;
        nrOfElements = jcfMap.length;
        
        this.corMap = corMap;
        xDim = corMap.length;
        yDim = corMap[0].length;
    }

    private ArrayList<Wse> buildWSEM(double[][] jcfMap){
        ArrayList<Wse> wses = new ArrayList<Wse>();
        for (int i = 0; i < jcfMap.length; i++) {
            double[] ds = jcfMap[i];
            Wse el = new Wse((int)ds[0],(int)ds[1],ds[2]);
            wses.add(el);
        }
        return wses;
    }

    public double[][] getSegmentationBorders(){
        ArrayList<Wse> wses = buildWSEM(jcfMap);
        Collections.sort(wses,new Comparator() {
               public int compare(Object o, Object o1) {
                return compare(((Wse)o).getZ(),((Wse)o1).getZ());
            }
        });
        
        ArrayList<ArrayList<Wse>> regions = new ArrayList<ArrayList<Wse>>();
        
        for (int i = 0; i < wses.size()-1; i++) {
            Wse e1 = wses.get(i);
            Wse e2 = wses.get(i+1);
                    
        }
        
        
        double[][] bordermap = new double[xDim][yDim];
        
        
        return bordermap;
    }

    private class Wse {
    //watershedelement
        private int x,y;
        private double z;

        public Wse(int x, int y, double z) {
            this.x = x;
            this.y = y;
            this.z = z;
        }

        public int getX() {
            return x;
        }

        public int getY() {
            return y;
        }

        public double getZ() {
            return z;
        }

    }

    private class Region{
        ArrayList<Wse> region = new ArrayList<Wse>();

        double xNeighborThreshold;
        double yNeighborThreshold;

        public Region(double xNeighborThreshold, double yNeighborThreshold) {
            this.xNeighborThreshold = xNeighborThreshold;
            this.yNeighborThreshold = yNeighborThreshold;
        }

        public void add(Wse wse){
            region.add(wse);
        }

        public boolean touches(Wse wse){
            for(Wse wseInRegion : region){
                if(     Math.abs(wse.getX()-wseInRegion.getX()) <= xNeighborThreshold &&
                        Math.abs(wse.getY()-wseInRegion.getY()) <= yNeighborThreshold)
                    return true;
            }
            return false;
        }
    }
}
